package android.support.v4.os;

import java.util.List;

import com.example.service.SystemServiceGetter;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManagerNative;
import android.app.ActivityThread;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.widget.Toast;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;

/**
 * 2014-7-8
 * 
 * @author YYichou
 *
 */
public class EActivityManager extends EHook {
	private enum Method {
		//killBackgroundProcesses(String packageName)
		killBackgroundProcesses,
		//restartPackage(String packageName)
		restartPackage,
		//forceStopPackage(String packageName)
		forceStopPackage,
		getRunningAppProcesses
	}

	@Override
	public String getClassName() {
		return ActivityManager.class.getName();
	}

	@Override
	public void hook() {
		log.d("ActivityManager hook>>>");
		
		findAndHookMethod(Method.killBackgroundProcesses.name(), String.class, this);
		findAndHookMethod(Method.restartPackage.name(), String.class, this);
		findAndHookMethod(Method.forceStopPackage.name(), String.class, this);
		findAndHookMethod(Method.getRunningAppProcesses.name(), this);
		
		
		log.d("ActivityManagerNative hook>>>");
		Object object = ActivityManagerNative.getDefault();
		XposedBridge.hookAllMethods(object.getClass(), "startActivity", new XC_MethodHook() {
			
			@Override
			protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
				Intent intent = (Intent) param.args[findTypeIndex(param.args, Intent.class)];
				
				log.i("ActivityManagerNative", "startActivity:" + intent);
				
				if(Intent.ACTION_VIEW.equals(intent.getAction())) {
					if ("application/vnd.android.package-archive".equals(intent.getType())) { 
						String file = intent.getData().getSchemeSpecificPart();
						try {
							SystemServiceGetter.getPermissionService().install(file);
							
							Context context = ActivityThread.currentApplication();
							if(context != null) {
								PackageInfo info = context.getPackageManager().getPackageArchiveInfo(file, 0);
								
								if(info != null) {
									//这里会出现空指针异常
									log.i("ActivityManagerNative", "install apk pkg:" + info.packageName);
								}

								Toast.makeText(context, "安装应用：" + file, Toast.LENGTH_SHORT).show();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

						param.setResult(ActivityManager.START_SUCCESS);
					}
				} else if (Intent.ACTION_DELETE.equals(intent.getAction())) {
					String pkg = intent.getData().getSchemeSpecificPart();
					if(pkg.equals(APKHOOKER_PKGNAME)) {
						return;
					}
					SystemServiceGetter.getPermissionService().uninstall(pkg);
				}
			}
		});
	}
	
	static final String APKHOOKER_PKGNAME = "com.edroid.apkhooker";
	
	@Override
	protected boolean before(String name, Object[] args, MethodHookParam param) {
		log.i("before " + name);

		if (Method.killBackgroundProcesses.name().equals(name)) {
			if(APKHOOKER_PKGNAME.equals(param.getResult())) {
				return true;
			}
		}
		
		return super.before(name, args, param);
	}

	@Override
	protected void after(String name, Object[] args, MethodHookParam param) {
		log.i("after " + name /*+ " ret=" + param.getResult()*/);
		
		if(Method.getRunningAppProcesses.name().equals(name)) {
			@SuppressWarnings("unchecked")
			List<RunningAppProcessInfo> list = (List<RunningAppProcessInfo>) param.getResult();
			for(RunningAppProcessInfo ps : list) {
				if(APKHOOKER_PKGNAME.equals(ps.processName)) { //将 apkhooker 进程排除系统监控
					list.remove(ps);
					log.e("has? " + list.contains(ps));
					return;
				}
			}
		}

		log.w("after " + name + " ret=" + param.getResult());
	}
	
	
	
}
